import 'package:flutter/material.dart';
import 'package:flutterapp/widget/Pager/CloudHelp.dart';
import 'package:flutterapp/widget/Pager/CloudPan.dart';
import 'package:flutterapp/widget/Pager/CloudPhone.dart';

class WScaffold extends StatefulWidget {
  @override
  _WScaffoldState createState() => _WScaffoldState();
}

class _WScaffoldState extends State<WScaffold> {
  int currentIndex = 0;
  PageController _controller;
  final List tabPages = [CloudPhone(), CloudPan(), CloudHelp()];
  final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(
        icon: Image.asset(
          'images/img/ic_main_tab_yunshouji.png',
          width: 24,
          height: 24,
        ),
        activeIcon: Image.asset(
          'images/img/ic_main_tab_yunshouji_pre.png',
          width: 24,
          height: 24,
        ),
        title: Text(
          "云手机",
        )),
    BottomNavigationBarItem(
        icon: Image.asset(
          'images/img/ic_main_tab_yunpan.png',
          width: 24,
          height: 24,
        ),
        activeIcon: Image.asset(
          'images/img/ic_main_tab_yunpan_pre.png',
          width: 24,
          height: 24,
        ),
        title: Text("云盘")),
    BottomNavigationBarItem(
        icon: Image.asset(
          'images/img/ic_main_tab_yunzhushou.png',
          width: 24,
          height: 24,
        ),
        activeIcon: Image.asset(
          'images/img/ic_main_tab_yunzhushou_pre.png',
          width: 24,
          height: 24,
        ),
        title: Text("云助手")),
  ];

  void _onTap(int index) {
    setState(() {
      currentIndex = index;
    });
    _controller.jumpToPage(index);
  }

  @override
  void initState() {
    super.initState();
    _controller = PageController(initialPage: currentIndex);
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }

  void _pageChange(int index) {
    if (index != currentIndex) {
      setState(() {
        currentIndex = index;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Scaffold"),
        centerTitle: true,
        automaticallyImplyLeading: false,
      ),
      body: PageView.builder(
        itemBuilder: (BuildContext context, int index) {
          return tabPages[index];
        },
        itemCount: tabPages.length,
        controller: _controller,
        physics: NeverScrollableScrollPhysics(),
        //禁止左右滑动
        onPageChanged: _pageChange,
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: bottomTabs,
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        fixedColor: Colors.purple,
        //选中文字颜色
        unselectedItemColor: Colors.red,
        //未选中文字颜色
        onTap: _onTap,
      ),
    );
  }
}
